package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.management.aop.anno.LogAnno;
import com.qk.management.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author 上官徵羽
 * @version 1.0
 * @since 2025-09-29 11:12
 */
@Slf4j
@SuppressWarnings("all")
@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @LogAnno
    @PutMapping
    public Result updateById(@RequestBody Dept dept){
        deptService.updateById(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查询部门id:{}", id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }
    @LogAnno
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        log.info("删除部门id:{}", id);
        deptService.delete(id);
        return Result.success();
    }

    @GetMapping
    public Result list(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize){
        PageResult<Dept> pageResult = deptService.page(name, status, page, pageSize);
    return Result.success(pageResult);
    }
    @LogAnno
    @PostMapping
    public Result addDept(@RequestBody Dept dept) {
        deptService.addDept(dept);
        return Result.success();
    }

    @GetMapping("/list")
    public Result listAll(){
        List<Dept> list = deptService.listAll();
        return Result.success(list);
    }
}
